//
//  TopicHeaderTableViewCell.swift
//  HoneyBee
//
//  Created by Smith_Yang on 2018/5/30.
//  Copyright © 2018 point. All rights reserved.
//

import UIKit
import SVProgressHUD
class TopicHeaderTableViewCell: UITableViewCell {
    weak var vc : UIViewController?
    typealias funcBlock = (_ isSelected : Bool) -> ()
    var blockFocus : funcBlock?
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var avatorAuthor: UIImageView!
    @IBOutlet weak var nameAuthor: UILabel!
    @IBOutlet weak var focusButton: UIButton!
    @IBOutlet weak var groupIcon: UIImageView!
    @IBOutlet weak var groupName: UILabel!
    
    var detailModel : TopicDetailModel?
    
    @IBAction func focusButtonAction(_ sender: Any) {
        
        guard let focusBlock = blockFocus else {
            return
        }
        if let model = detailModel {
            focusBlock(model.concern)
        }
        
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        selectionStyle = .none
        
        groupIcon.image = UIImage.icon(from: kIcon_group, color: kColorTextPlacehold, imageSize: CGSize(width: 15, height: 13)).withRenderingMode(.alwaysOriginal)
        
        avatorAuthor.layer.cornerRadius = avatorAuthor.frame.size.height/2.0
        
        focusButton.layer.cornerRadius = focusButton.frame.size.height/2.0
        focusButton.layer.borderWidth = 0.5
        focusButton.layer.borderColor = UIColor(hex:"#E51616")?.cgColor

        let tap = UITapGestureRecognizer.init(target: self, action: #selector(didTap(tap:)))
        groupName.isUserInteractionEnabled = true
        groupName.addGestureRecognizer(tap)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    
    public func configModel(model:TopicDetailModel) {
        
        detailModel = model;
        
        titleLabel.text = model.title
        
        avatorAuthor.showImageS(model.authorHeadImg)
        avatorAuthor.backgroundColor = UIColor(hex: "#999999")
        avatorAuthor.layer.cornerRadius = avatorAuthor.frame.size.height/2.0
        avatorAuthor.clipsToBounds = true
        
        nameAuthor.text = model.authorName
        
        groupName.text = model.groupName
        
        if (model.concern){
            self.focusButton.backgroundColor = UIColor.white
            self.focusButton.setTitle("已关注", for: .normal)
            self.focusButton.setTitleColor(UIColor(hex:"#E51616"), for: .normal)
        } else {
            self.focusButton.backgroundColor = UIColor(hex:"#E51616")
            self.focusButton.setTitle("关注", for: .normal)
            self.focusButton.setTitleColor(UIColor.white, for: .normal)
        }
        
    }
    @objc func didTap(tap:UITapGestureRecognizer)  {
        let groupVC = GroupTopicViewController();
        let topicModel = TopicGroupModel.init()
        topicModel.groupId = (detailModel?.groupId)!;
        topicModel.groupName = (detailModel?.groupName)!
        groupVC.configModel(model: topicModel)
        self.vc?.navigationController?.pushViewController(groupVC, animated: true)

//        let dict = [
//            "userId":String(UserInfo.shareInstance.userModel().id),
//            "groupId":(detailModel!.groupId),
//            "isDelete":false,
//            "timestamp":1,
//            "sign" : 1,
//            "operatorId" : UserInfo.shareInstance.userModel().id ] as [String : Any]
//
//        TempNetTool.post( kHost + kquitOrJoinGroupList + dict.formatApi(), parameters: nil, progress: nil, success: { [weak self] (result) in
//            if (result != nil) {
//                guard let resultDict = result as? [String : NSObject] else { return }
//                guard let resCode = resultDict["resCode"] as? String else { return }
//                if (resCode == "200"){
//                    SVProgressHUD.showInfo(withStatus: "您已经成功加入该群组")
//                    SVProgressHUD.dismiss(withDelay: 1.5)
//                }
//            }
//
//        }) {[weak self]   (error) in
//
//        }
    }

}
